package com.vancouver.knows.ui.fragment.my;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.vancouver.knows.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Chris Li on 2016-07-22.
 */
public class MenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private LayoutInflater mLayoutInflater;
    private List<MenuItem> mMenuList;
    private OnMenuItemClickListener mListener;
    private HashMap<Integer, String> mNotificationCount = new HashMap<>();

    public MenuAdapter(Context context, List<MenuItem> menuItems) {
        this.mLayoutInflater = LayoutInflater.from(context);
        this.mMenuList = menuItems;

        for (MenuItem menuItem : menuItems) {
            if (menuItem.hasNotification) {
                mNotificationCount.put(menuItem.getTitleResource(), "0");
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == MenuItem.MENU_ITEM_HEADER) {
            View view = mLayoutInflater.inflate(R.layout.view_user_menu_header, parent, false);
            return new MenuItemViewHolder(view);
        } else {
            View view = mLayoutInflater.inflate(R.layout.result_view_item, parent, false);
            return new MenuItemViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final MenuItem menuItem = mMenuList.get(position);
        MenuItemViewHolder menuItemViewHolder = (MenuItemViewHolder) holder;

        if (menuItem.actionType == MenuItem.MENU_ITEM_HEADER) {
            menuItemViewHolder.title.setText(menuItem.getTitleResource());
        } else if (menuItem.actionType == MenuItem.MENU_ITEM_DEFAULT) {
            menuItemViewHolder.title.setText(menuItem.getTitleResource());
            if (menuItemViewHolder.chevron != null) {
                menuItemViewHolder.chevron.setVisibility(View.VISIBLE);
            }
            if (menuItemViewHolder.icon != null) {
                menuItemViewHolder.icon.setImageResource(menuItem.getIconResource());
            }
        } else if (menuItem.actionType == MenuItem.MENU_ITEM_NORMAL) {
            menuItemViewHolder.title.setText(menuItem.getTitleResource());
            if (menuItemViewHolder.chevron != null) {
                menuItemViewHolder.chevron.setVisibility(View.GONE);
            }
            if (menuItemViewHolder.icon != null) {
                menuItemViewHolder.icon.setImageResource(menuItem.getIconResource());
            }
        }

        if (menuItemViewHolder.notification != null) {
            if (menuItem.hasNotification) {
                String count = mNotificationCount.get(menuItem.getTitleResource());
                if (count.equals("0")) {
                    menuItemViewHolder.notification.setVisibility(View.GONE);
                } else {
                    menuItemViewHolder.notification.setVisibility(View.VISIBLE);
                    menuItemViewHolder.notification.setText(count);
                }
            } else {
                menuItemViewHolder.notification.setVisibility(View.GONE);
            }
        }

        menuItemViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onMenuItemClicked(menuItem);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        return mMenuList.get(position).getActionType();
    }

    @Override
    public int getItemCount() {
        return mMenuList.size();
    }

    public void setListener(OnMenuItemClickListener listener) {
        this.mListener = listener;
    }

    public void setNotification(int menuItemId, String count) {
        mNotificationCount.put(menuItemId, count);
        notifyDataSetChanged();
    }

    public void resetNotifications() {
        for (Map.Entry<Integer, String> entry : mNotificationCount.entrySet()) {
            entry.setValue("0");
        }

        notifyDataSetChanged();
    }

    public interface OnMenuItemClickListener {
        void onMenuItemClicked(MenuItem menuItem);
    }

    public static class MenuItemViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.title)
        TextView title;
        @Nullable
        @Bind(R.id.icon)
        ImageView icon;
        @Nullable @Bind(R.id.chevron)
        ImageView chevron;
        @Nullable @Bind(R.id.notification)
        TextView notification;

        public MenuItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class MenuItem {

        public static final int MENU_ITEM_HEADER = 0;
        public static final int MENU_ITEM_DEFAULT = 1;
        public static final int MENU_ITEM_NORMAL = 2;

        private int iconResource;
        private int titleResource;
        private int actionType;
        private boolean hasNotification = false;

        public MenuItem(int iconResource, int titleResource, int actionType) {
            this.iconResource = iconResource;
            this.titleResource = titleResource;
            this.actionType = actionType;
        }

        public int getTitleResource() {
            return titleResource;
        }

        public void setTitleResource(int titleResource) {
            this.titleResource = titleResource;
        }

        public int getActionType() {
            return actionType;
        }

        public void setActionType(int actionType) {
            this.actionType = actionType;
        }

        public int getIconResource() {
            return iconResource;
        }

        public void setIconResource(int iconResource) {
            this.iconResource = iconResource;
        }


        public boolean hasNotification() {
            return hasNotification;
        }

        public void setHasNotification(boolean hasNotification) {
            this.hasNotification = hasNotification;
        }
    }
}